package com.tang.common.utils.excel.handler;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.tang.common.utils.CoolUtil;
import com.tang.common.model.res.ResSysDictValueTableItem;
import com.tang.common.utils.excel.annotation.ExcelSelect;
import jakarta.annotation.Resource;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.redisson.api.RedissonClient;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author tang jun liang
 * @Date 2023/9/5 21:15
 */
public abstract class AbsExcelSelectHandler<T> implements SheetWriteHandler {


    @Resource
    protected RedissonClient redissonClient;


    /**
     * 获取单元格需要下拉框的
     *
     * @return
     */
    protected Map<Integer, List<String>> getCellSelectMap() {
        Class<T> domainClass = getDomainClass();
        Map<Integer, List<String>> cellSelectMap = new HashMap<>();
        Field[] declaredFields = domainClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(ExcelSelect.class)) {
                ExcelProperty excelProperty = declaredField.getAnnotation(ExcelProperty.class);
                ExcelSelect excelSelect = declaredField.getAnnotation(ExcelSelect.class);
                if (declaredField.getType().isEnum()) {
                    String enumName = declaredField.getType().getSimpleName();
                    List<ResSysDictValueTableItem> valueTableItemList = redissonClient.getList(CoolUtil.camelToUnderline(enumName));
                    List<String> labelList = valueTableItemList.stream().map(ResSysDictValueTableItem::getDesc).collect(Collectors.toList());
                    cellSelectMap.put(excelProperty.index(), labelList);
                } else if (declaredField.getType().equals(Boolean.class)) {
                    cellSelectMap.put(excelProperty.index(), List.of("是", "否"));
                }
            }
        }
        return cellSelectMap;
    }

    /**
     * 默认增加下拉框
     * @param writeWorkbookHolder
     * @param writeSheetHolder
     */
    protected  void afterSheetCreate0(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder){
        Map<Integer, List<String>> cellSelectMap = getCellSelectMap();
        Sheet sheet = writeSheetHolder.getSheet();
        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        cellSelectMap.forEach((k, v) -> {
            CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, k, k);
            //设置下拉框数据
            DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(v.toArray(new String[0]));
            DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
            sheet.addValidationData(validation);
        });
    }

    protected abstract Class<T> getDomainClass();


    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        //调用默认的下拉框
        this.afterSheetCreate0(writeWorkbookHolder, writeSheetHolder);
    }
}
